package com.schoolgard.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.AlarmLogService;
import com.schoolegard.services.CatalogService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.Alarm4MapBean;
import com.schoolgard.entitybean.Catalog;
import com.schoolgard.entitybean.NavTreeEntity;
import com.supersystem.utils.TypeUtils;



public class MainAction extends BaseAction{
    
	private static final long serialVersionUID = 201111302313L;
	
	private CatalogService catalogService;
	private AlarmLogService alarmLogService;
	public void setCatalogService(CatalogService catalogService) {
		this.catalogService = catalogService;
	}
	public void setAlarmLogService(AlarmLogService alarmLogService) {
		this.alarmLogService = alarmLogService;
	}

	private String nav_id;
	private List<Catalog> navCatalog;
	private List<NavTreeEntity> navTreeBean;
	
	
	/**
	 * 初始化顶级菜单 
	 * @return
	 */
	public String initNavMenue()
	{
		Object catalogId = getSession().get(TypeUtils.USER_CATALOG_IDS);
		if(catalogId != null && StringUtils.isNotBlank(catalogId.toString())){
			navCatalog = catalogService.parseCatalog(catalogId.toString() , 1 , 1); //查找第一层的菜单
			if(navCatalog != null && navCatalog.size() > 0) {
				nav_id = navCatalog.get(0).getId();
			}else{
				nav_id = findDefaultHeaderMenu();
			}
		}else{
			navCatalog = new ArrayList<Catalog>(0);
		}
		return "header";
	}
	
	private String findDefaultHeaderMenu()
	{
		List<Catalog> catalogs = catalogService.findCatalogByLevel(1); 
		if(catalogs != null && catalogs.size() > 0){
			return catalogs.get(0).getId();
		}else{
			return null;
		}
	}
	
	/**
	 * 初始化左边导航树
	 * @return
	 */
	public String initLeftMenu()
	{
		nav_id = request.getParameter("nav_id");
		//如果没有传入id，则默认显示当前用户所拥有的菜单的第一个菜单下的子节点
		if(StringUtils.isBlank(nav_id)){
			Object catalogId = getSession().get(TypeUtils.USER_CATALOG_IDS);
			if(catalogId != null && StringUtils.isNotBlank(catalogId.toString())){
				String [] ids = catalogId.toString().split(",");
				if(ids.length > 0)
				nav_id = ids[0];
			}
		}
		navTreeBean = findLeftTreeByParentId(nav_id);
		return "navigateTree";
	}
	
	/**
	 * 通过父ID查找树的所有节点
	 * @param parentId
	 * @return
	 */
	public List<NavTreeEntity> findLeftTreeByParentId(String parentId)
	{
		List<NavTreeEntity> navTreeArray = new ArrayList<NavTreeEntity>();
		Object catalogIds = session.get(TypeUtils.USER_CATALOG_IDS); //更新菜单之后要重新登录
		if(StringUtils.isBlank(parentId)){
		    return null;
		}else{
			List<Catalog> catalogs = catalogService.findCatalogByParentId(parentId);
			for(int i=0 ; i<catalogs.size() ; i++){
				NavTreeEntity navTree = new NavTreeEntity();
				Catalog catalog = catalogs.get(i);
				if(catalogIds != null && (catalogIds.toString().indexOf(catalog.getId()+",") != -1
					|| catalogIds.toString().indexOf(", "+catalog.getId()) != -1))//权限里面找不到记录则跳过
				{
					navTree.setCatalog(catalog);
					List<NavTreeEntity> children = findLeftTreeByParentId(catalog.getId());
					
					if(children == null && catalog.getIsleaf()){
						navTree.setChildren(null);
					}else{
						navTree.setChildren(children);
					}
					navTreeArray.add(navTree);
				}
			}
		}
		return navTreeArray;
	}
	
	/**
	 * 查找报警数据，给地图使用
	 */
	public void findAlarm4map()
	{
		try{
	   	List<Alarm4MapBean> al4mb = alarmLogService.findAlarm4Map();
	   	String jsonStr = JSONArray.fromObject(al4mb).toString();
	   	writeToResponse(jsonStr);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String getNav_id() {
		return nav_id;
	}
	public void setNav_id(String nav_id) {
		this.nav_id = nav_id;
	}
	public List<Catalog> getNavCatalog() {
		return navCatalog;
	}
	public void setNavCatalog(List<Catalog> navCatalog) {
		this.navCatalog = navCatalog;
	}
	public List<NavTreeEntity> getNavTreeBean() {
		return navTreeBean;
	}
	public void setNavTreeBean(List<NavTreeEntity> navTreeBean) {
		this.navTreeBean = navTreeBean;
	}

}
